angular.module('music_player', [])
    .controller('music_playerCtrl', function ($rootScope, $scope) {
        $rootScope.playList = [
            {
                title: "Alan Walker - Faded.mp3",
                artist: "ADG3",
                mp3: "http://127.0.0.1/MusicWeb/File_fileDownload.action?filePath=E:/KuGou/Alan Walker - Faded.mp3",
                poster: "images/m0.jpg"
            }, {
                title: "Alan Walker - Sing Me to Sleep.mp3",
                artist: "ADG3",
                mp3: "http://127.0.0.1/MusicWeb/File_fileDownload.action?filePath=E:/KuGou/Alan Walker - Sing Me to Sleep.mp3",
                poster: "images/m0.jpg"
            }, {
                title: "周杰伦 - 稻香.mp3",
                artist: "ADG3",
                mp3: "http://127.0.0.1/MusicWeb/File_fileDownload.action?filePath=E:/KuGou/周杰伦 - 稻香.mp3",
                poster: "images/m0.jpg"
            }
        ];

        //添加歌曲
        $rootScope.addSong = function (item) {
            var song = {
                title: item.title,
                mp3: item.file_link,
                poster: item.pic_big
            };
            var flag = true;
            angular.forEach($rootScope.myPlaylist.playlist, function (palySong) {
                if(palySong.title == song.title){
                    flag = false;
                    return;
                }
            });
            if(flag){
                $rootScope.myPlaylist.add(song);
                alert('添加成功！');
            } else {
                alert('当前歌曲已添加！');
            }
        };

        //添加并播放当前歌曲
        $rootScope.playCurrentSong = function (item) {
            var song = {
                title: item.title,
                mp3: item.file_link,
                poster: item.pic_big
            };
            $rootScope.addSong(item);
            angular.forEach($rootScope.myPlaylist.playlist, function (palySong) {
                if(palySong.title == song.title){
                    $rootScope.myPlaylist.play($rootScope.myPlaylist.playlist.indexOf(palySong));
                    return;
                }
            });
        };

        //初始化播放器
        init();
        function init(){
            $rootScope.myPlaylist = new jPlayerPlaylist({
                    jPlayer: "#jplayer_N",
                    cssSelectorAncestor: "#jp_container_N"
                },
                $rootScope.playList
                , {
                    playlistOptions: {
                        enableRemoveControls: true,
                        autoPlay: true
                    },
                    swfPath: "js/jPlayer",
                    supplied: "webmv, ogv, m4v, oga, mp3",
                    smoothPlayBar: true,
                    keyEnabled: true,
                    audioFullScreen: false
                });
        }

        $(document).on($.jPlayer.event.pause, $rootScope.myPlaylist.cssSelector.jPlayer, function () {
            $('.musicbar').removeClass('animate');
            $('.jp-play-me').removeClass('active');
            $('.jp-play-me').parent('li').removeClass('active');
        });

        $(document).on($.jPlayer.event.play, $rootScope.myPlaylist.cssSelector.jPlayer, function () {
            $('.musicbar').addClass('animate');
        });

        $(document).on('click', '.jp-play-me', function (e) {
            e && e.preventDefault();
            var $this = $(e.target);
            if (!$this.is('a')) $this = $this.closest('a');

            $('.jp-play-me').not($this).removeClass('active');
            $('.jp-play-me').parent('li').not($this.parent('li')).removeClass('active');

            $this.toggleClass('active');
            $this.parent('li').toggleClass('active');
            if (!$this.hasClass('active')) {
                $rootScope.myPlaylist.pause();
            } else {
                var i = Math.floor(Math.random() * (1 + 7 - 1));
                $rootScope.myPlaylist.play(i);
            }

        });
    });